using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using AillieoUtils;
public class DynimacLogVC : MonoBehaviour
{
    public RectTransform templateTextItem;
    private RectTransform templateTextItemInstance;
    public ScrollView scrollView;
    
    List<DefaultScrollItemData> logData = new List<DefaultScrollItemData>();
    void updateFunc(int index, RectTransform item)
    {
        DefaultScrollItemData data = this.logData[index];
        item.GetComponent<Text>().text = string.Format("{0} : {1}", index, data.longString);
    }
    int itemCountFunc()
    {
        return this.logData.Count;
    }
    
    Vector2 itemSizeFunc(int index)
    {
        if (this.templateTextItemInstance == null)
        {
            this.templateTextItemInstance = GameObject.Instantiate(this.templateTextItem).GetComponent<RectTransform>();
            this.templateTextItemInstance.gameObject.SetActive(true);
            this.templateTextItemInstance.localScale = Vector3.zero;
        }
        this.templateTextItemInstance.GetComponent<Text>().text = this.logData[index].longString;
        LayoutRebuilder.ForceRebuildLayoutImmediate(this.templateTextItemInstance);
        var height = LayoutUtility.GetPreferredHeight(this.templateTextItemInstance);
        return new Vector2(1100, height);
    }

    void Start()
    {
        this.scrollView.SetUpdateFunc(this.updateFunc);
        this.scrollView.SetItemSizeFunc(this.itemSizeFunc);
        this.scrollView.SetItemCountFunc(this.itemCountFunc);
        
        var dataCount = 0;
        do
        {
            this.AppendData("BeginLog...");
        }
        while (++dataCount < 50);
        
        this.scrollView.UpdateData(false);
    }
    
    void UpdateAllScrollViewsIncrementally()
    {
        this.scrollView.UpdateDataIncrementally(false);
        this.scrollView.ScrollTo(this.logData.Count-1);
    }
    public void AppendData(string log)
    {
        var newData = new DefaultScrollItemData() { name = "logData.Count.ToString()", longString = log };
        this.logData.Add(newData);
        this.UpdateAllScrollViewsIncrementally();
    }
}
